//myTest

package ucl.cs.bank.client;
/**
 * Example Bank Client Class.
 * @author  K. Bryson.
 * @version 1.0
 */
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import ucl.cs.bank.common.*;

public class BankClient { 

	public static void main(String[] args) { 
		try { 
			if (args.length == 0) {
				printUsage();
				System.exit(1);
			}

			// Currently assumes localhost.
			Registry registry  = LocateRegistry.getRegistry("localhost");
			UCLBank  bank      = (UCLBank) registry.lookup("UCLBankServer");

			if (args[0].equals("create")) {
				if (args.length != 2) {
					System.out.println("usage: java BankClient create <account_name>");
					System.exit(1);
				}

				int account_number = bank.createAccount(args[1]);
				System.out.println(" Created account number = " + account_number);

			} else if (args[0].equals("delete")) {

				if (args.length != 2) {
					System.out.println("usage: java BankClient delete <account_number>");
					System.exit(1);
				}

				int account_number = Integer.parseInt(args[1]);
				bank.deleteAccount(account_number);
				System.out.println(" Deleted account number = " + account_number);

			} else if (args[0].equals("info")) {

				if (args.length != 2) {
					System.out.println("usage: java BankClient info <account_number>");
					System.exit(1);
				}

				int account_number = Integer.parseInt(args[1]);
				UCLAccount account = bank.getAccount(account_number);
				String info = account.getDescription();
				System.out.println(info);

			} else if (args[0].equals("list")) {

				if (args.length != 1) {
					System.out.println("usage: java BankClient list");
					System.exit(1);
				}
				
				
				UCLAccountInfo[] info_array = bank.listAccounts();
				
				for (int i = 0; i < info_array.length; i++) {
					System.out.println(info_array[i]);
				}

			} else if (args[0].equals("deposit")) {

				if (args.length != 3) {
					System.out.println("usage: java BankClient deposit <account_number> <amount>");
					System.exit(1);
				}

				int account_number = Integer.parseInt(args[1]);
				float amount = Float.parseFloat(args[2]);
				UCLAccount account = bank.getAccount(account_number);
				account.deposit(amount);
				System.out.println(" Deposited " + amount +
						" into account number = " + account_number);

			} else if (args[0].equals("withdraw")) {

				if (args.length != 3) {
					System.out.println("usage: java BankClient withdraw <account_number> <amount>");
					System.exit(1);
				}

				int account_number = Integer.parseInt(args[1]);
				float amount = Float.parseFloat(args[2]);
				UCLAccount account = bank.getAccount(account_number);
				account.withdraw(amount);
				System.out.println(" Withdrawed " + amount +
						" from account number = " + account_number);

			} else {
				printUsage();
				System.exit(1);
			}
		}
		catch (NoSuchAccountException except) {
			System.out.println(except);
		}
		catch (java.rmi.RemoteException re) {
			System.out.println(); 
			System.out.println( "RemoteException");
			System.out.println(re);
		} 
		catch (java.rmi.NotBoundException nbe) {
			System.out.println(); 
			System.out.println( "NotBoundException");
			System.out.println(nbe);
		} 
	}

	private static void printUsage() {
		System.out.println("usage: java BankClient create <account_name>");
		System.out.println("usage: java BankClient delete <account_number>");
		System.out.println("usage: java BankClient info   <account_number>");
		System.out.println("usage: java BankClient list");
		System.out.println("usage: java BankClient deposit <account_number> <amount>");
		System.out.println("usage: java BankClient withdraw <account_number> <amount>");
	}
} 

